<?php
/*
 * litchicms
 * Copyright (C) 2010-5-1 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

!defined('BRK') && exit("!");

/*
 * template_parse_cycle 解析
 * @return $string
*/

function template_parse_cycle($string) {

    $all_cycle=array();

    preg_match_all("/\{cycle\s([^\{]*?)\}/is", $string  ,$all_cycle);

    if(count($all_cycle[0])==0) return $string;

    $replacement=array();

    foreach ($all_cycle[1] as $k=>&$v) {
        
        $temparr=template_k2v($v);
        
        $tempstring='';

        if(empty($temparr['values']))   brk_log(0, get_lang("tpl_syntax").$all_cycle[0][$k]);

        $tempstring.="<?php if(empty(\$cycle_".$k.")) {";
        $tempstring.="\$cycle_$k=array("."\"".str_replace(",", "\",\"", $temparr['values'])."\"".");";
        $tempstring.="\$cycle_key_$k=0;";
        $tempstring.="\$cycle_count_$k=count(\$cycle_$k);";
        $tempstring.="}";
        $tempstring.="if(\$cycle_key_$k==\$cycle_count_$k) {";
        $tempstring.="\$cycle_key_$k=0;";
        $tempstring.="}";
        $tempstring.="echo \$cycle_".$k."[\$cycle_key_$k++];";
        $tempstring.="?>";
        
        $replacement[]=$tempstring;
    }


    
    template_arr2patt($all_cycle[0]);
    $string=preg_replace($all_cycle[0], $replacement, $string,1);

    return $string;
    
}
?>